package com.weixin.engine.controller;

import cn.hutool.core.util.RandomUtil;
import com.jcraft.jsch.SftpException;
import com.weixin.common.constant.GlobalConstant;
import com.weixin.common.sftp.HostHolder;
import com.weixin.common.sftp.SftpPool;
import com.weixin.common.sftp.SftpTemplate;
import com.weixin.common.sftp.SftpTest;
import org.apache.commons.pool2.impl.DefaultPooledObjectInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author wangchunhui
 * @date 2022/10/24 10:57
 */
@RestController
@RequestMapping("/sftp")
public class SftpController {

    @Resource
    private SftpTest sftpTest;

    @Resource
    private SftpPool sftpPool;

    @Resource
    private SftpTemplate sftpTemplate;

    /**
     * sftp demo
     *
     * @author wangchunhui
     * @date 2023/6/13 16:47
     */
    @GetMapping("/pressure/test")
    public void fileDownload() {
        List<String> hostNames = new ArrayList<>(Arrays.asList("smy", "hzyf", "vcredit"));
        Map<String, String> map = new HashMap<>();
        map.put("smy", "/upload/dotnet6.zip");
        map.put("hzyf", "/upload/dotnet6.zip");
        map.put("vcredit", "/upload/report/test.pdf");
        String host = hostNames.get(RandomUtil.randomInt(3));
        String path = map.get(host);
        sftpTest.multiHost(host, path);
        Map<String, List<DefaultPooledObjectInfo>> listAllObjects = sftpPool.getInternalKeyedPool().listAllObjects();
        for (Map.Entry<String, List<DefaultPooledObjectInfo>> next : listAllObjects.entrySet()) {
            String key = next.getKey();
            List<DefaultPooledObjectInfo> value = next.getValue();
            System.out.println("curkey=" + key + ",size=" + value.size());
        }
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String hostName, @RequestParam String path) throws SftpException, IOException {
        HostHolder.changeHost(hostName);
        String fileName = path.substring(path.lastIndexOf("/") + 1);
        InputStream inputStream = sftpTemplate.download(path);
        //设置文件类型
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        //设置编码格式
        response.setCharacterEncoding(GlobalConstant.UTF8);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format(GlobalConstant.HEADER, fileName));

        byte[] buffer = new byte[4096];
        int bytesRead;
        ServletOutputStream outputStream = response.getOutputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
    }
}
